package com.qy139.sso.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qy139.sso.util.MapperUtils;
import com.qy139.sso.util.OkHttpClientUtil;
import com.qy139.util.CommUtil;
import com.qy139.util.Result;
import lombok.SneakyThrows;
import okhttp3.Response;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
public class MySuccessHandler implements AuthenticationSuccessHandler {

    @Resource
    private UserDetailsService userDetailsService;

    @SneakyThrows
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String username = httpServletRequest.getParameter("username");
        String password = httpServletRequest.getParameter("password");
        User user = (User) userDetailsService.loadUserByUsername(username);
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        ObjectMapper objectMapper = new ObjectMapper();
        Result result = new Result();
        result.setCode(200);
        result.setMsg("登陆成功");
        Map map = new HashMap();
        map.put("username",username);
        map.put("password",password);
        map.put("client_id", CommUtil.CLIENT_ID);
        map.put("client_secret",CommUtil.CLIENT_SECRET);
        map.put("grant_type",CommUtil.PASSWORD);

        //token 获取出来
        // localhost:8088/oauth/token?username=张三&password=123456&client_id=client_id&client_secret=secret&grant_type=password
        Response response = OkHttpClientUtil.getInstance().postData("http://localhost:8082/oauth/token", map);
        String s1 = Objects.requireNonNull(response.body()).string();
       // System.out.println("response的信息==="+s1);
        Map<String, Object> jsonMap = MapperUtils.json2map(s1);
        Object token = jsonMap.get("access_token");// 对应的token的值
        Object user1 = jsonMap.get("user");
        //System.out.println("对应的token的值=====:"+token);

        Map maps = new HashMap();
        maps.put("token",token);
        maps.put("user",user1);
        maps.put("authorities",authorities);
        result.setData(maps);
        String s = objectMapper.writeValueAsString(result);
        httpServletResponse.setContentType("application/json;charset=utf8");

        PrintWriter writer = httpServletResponse.getWriter();
        writer.print(s);
        writer.flush();
        writer.close();
    }
}
